﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsRemoteScripting.Action
{
    public class ActionAutoSave : AbstractCommand
    {
        private Timer m_Timer = new Timer();

        private int m_nInterval = (int)new TimeSpan(0, 5, 0).TotalMilliseconds;

        public ActionAutoSave()
        {
        }

        public override void Run(object sender, EventArgs e)
        {
            Form1.m_Form.autosaveToolStripMenuItem.Checked = !Form1.m_Form.autosaveToolStripMenuItem.Checked;
            m_Timer.Tick += m_Timer_Tick;

            if (Form1.m_Form.autosaveToolStripMenuItem.Checked)
            {
                m_Timer.Start();
            }
            else
            {
                m_Timer.Stop();
            }
        }

        public int Interval
        {
            get { return m_nInterval; }
            set { m_nInterval = value; }
        }

        public void SetIntervalTimeSpan(TimeSpan _timeSpan)
        {
            m_nInterval = _timeSpan.Milliseconds;
        }

        void m_Timer_Tick(object sender, EventArgs e)
        {
            TimerFunction(sender, e);
        }

        private static void TimerFunction(object sender, EventArgs e)
        {
            SaveFile(sender, e);
        }

        private static void SaveFile(object sender, EventArgs e)
        {
            Save save = new Save();

            DateTime dateTime = DateTime.Now;
            CreateFileName(save, dateTime);
            save.UseFileDialog = false;
            save.Run(sender, e);
        }

        private static void CreateFileName(Save save, DateTime dateTime)
        {
            save.FileName = string.Format("{0}-{1}-{2}-{3}-{4}-{5}.wrs.autosave", dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
        }
    }
}
